#include<bits/stdc++.h>
using namespace std;
#define pc putchar
#define pb push_back
#define mp make_pair
#define int long long
#define ls (id<<1)
#define rs ((id<<1)|1)
#define mid ((l+r)>>1)
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
// #define gc getchar
char buf[(1<<21)+5],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)

int read(){
    int ret=0,f=0; char ch=gc();
    while(ch<'0'||ch>'9') f|=(ch=='-'),ch=gc();
    while(ch>='0'&&ch<='9') ret=(ret<<1)+(ret<<3)+(ch^48),ch=gc();
    return f?-ret:ret;
}

void write(int x){
    if(!x) return pc('0'),void();
    if(x<0) pc('-'),x=-x;
    int stk[30],tp=0;
    while(x) stk[++tp]=x%10,x/=10;
    while(tp) pc('0'+stk[tp--]);
    return;
}

const int Maxn=1010,Mod=147744151;
int N,M,P,T[Maxn][Maxn],A[Maxn][Maxn],Q,Ans;

int exgcd(int a,int b,int &x,int &y){
    if(!b){ x=1,y=0; return a;}
    int t=exgcd(b,a%b,y,x);
    y-=(a/b)*x; return t;
}

signed main(){
    freopen("tsuki.in","r",stdin);
    freopen("tsuki.out","w",stdout);
    N=read(),M=read(),P=read();
    For(i,0,N) For(j,0,M) T[i][j]=1;
    For(i,1,N) For(j,1,M) A[i][j]=read();
    // For(i,1,N) For(j,1,M){
    //     int a=T[i-1][j-1],x=0,y=0;
    //     exgcd(a,P,x,y); int inv=(x+P)%P;
    //     T[i][j]=T[i-1][j]*T[i][j-1]%P*inv%P*A[i][j]%P;
    // }

    Q=read();
    For(i,1,Q){
        int xa=read(),ya=read(),xb=read(),yb=read(),sol=1;
        For(x,xa,xb){
            For(y,ya,yb){
                (sol*=A[x][y])%=P;
            }
        }
        (Ans+=i^(sol%P))%=Mod;
        // int sol=T[xb][yb]*T[xa-1][ya-1]%P;
        // int t1=T[xa-1][yb],t2=T[xb][ya-1];
        // int inv1,inv2,c1,c2;
        // exgcd(t1,P,inv1,c1),exgcd(t2,P,inv2,c2);
        // (inv1+=P)%=P,(inv2+=P)%=P;
        // (sol*=inv1*inv2%P)%=P;
        // (Ans+=i^(sol%P))%=Mod;
    }
    write(Ans);
    return 0;
}